//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CreditDefaultSwap.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/CashFlow.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Instruments/Claim.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QL::Protection::SideEnum side, Double notional, Double upfront, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ upfrontDate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CCreditDefaultSwap::typeid)
{
    CSchedule^ _Cschedule;
    CDayCounter^ _CdayCounter;
    CClaim^ _Cclaim;
    try
    {
#ifdef HANDLE
        _phCreditDefaultSwap = NULL;
#endif
        QuantLib::Protection::Side _side = (QuantLib::Protection::Side)side ;
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional);
        QuantLib::Rate _upfront = (QuantLib::Rate)ValueHelper::Convert (upfront);
        QuantLib::Rate _spread = (QuantLib::Rate)ValueHelper::Convert (spread);
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        QuantLib::Date _protectionStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (protectionStart) ? (QuantLib::Date)ValueHelper::Convert (protectionStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _upfrontDate = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (upfrontDate) ? (QuantLib::Date)ValueHelper::Convert (upfrontDate->Value) : QuantLib::Date()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>::IsSome::get (claim))
        {
            _Cclaim = safe_cast<CClaim^> (claim->Value);
            _Cclaim->Lock();
        }
        boost::shared_ptr<QuantLib::Claim>& _claim = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>::IsSome::get (claim) ? static_cast<boost::shared_ptr<QuantLib::Claim>&> (_Cclaim->GetShared ()) : boost::shared_ptr<QuantLib::Claim>()); //1
        _ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (new QuantLib::CreditDefaultSwap ( _side,  _notional,  _upfront,  _spread,  _schedule,  _paymentConvention,  _dayCounter,  _settlesAccrual,  _paysAtDefaultTime,  _protectionStart,  _upfrontDate,  _claim ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCreditDefaultSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cclaim != nullptr) _Cclaim->Unlock();
    }
}
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QL::Protection::SideEnum side, Double notional, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CCreditDefaultSwap::typeid)
{
    CSchedule^ _Cschedule;
    CDayCounter^ _CdayCounter;
    CClaim^ _Cclaim;
    try
    {
#ifdef HANDLE
        _phCreditDefaultSwap = NULL;
#endif
        QuantLib::Protection::Side _side = (QuantLib::Protection::Side)side ;
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional);
        QuantLib::Rate _spread = (QuantLib::Rate)ValueHelper::Convert (spread);
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        bool _settlesAccrual = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (settlesAccrual) ? (bool)ValueHelper::Convert (settlesAccrual->Value) : true); //4
        bool _paysAtDefaultTime = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (paysAtDefaultTime) ? (bool)ValueHelper::Convert (paysAtDefaultTime->Value) : true); //4
        QuantLib::Date _protectionStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (protectionStart) ? (QuantLib::Date)ValueHelper::Convert (protectionStart->Value) : QuantLib::Date()); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>::IsSome::get (claim))
        {
            _Cclaim = safe_cast<CClaim^> (claim->Value);
            _Cclaim->Lock();
        }
        boost::shared_ptr<QuantLib::Claim>& _claim = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>::IsSome::get (claim) ? static_cast<boost::shared_ptr<QuantLib::Claim>&> (_Cclaim->GetShared ()) : boost::shared_ptr<QuantLib::Claim>()); //1
        _ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (new QuantLib::CreditDefaultSwap ( _side,  _notional,  _spread,  _schedule,  _paymentConvention,  _dayCounter,  _settlesAccrual,  _paysAtDefaultTime,  _protectionStart,  _claim ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCreditDefaultSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cclaim != nullptr) _Cclaim->Unlock();
    }
}
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (boost::shared_ptr<QuantLib::CreditDefaultSwap>& childNative, Object^ owner) : CInstrument(CCreditDefaultSwap::typeid)
{
#ifdef HANDLE
	_phCreditDefaultSwap = NULL;
#endif
	_ppCreditDefaultSwap = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
}
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QuantLib::CreditDefaultSwap& childNative, Object^ owner) : CInstrument(CCreditDefaultSwap::typeid)
{
#ifdef HANDLE
	_phCreditDefaultSwap = NULL;
#endif
	_ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
    _CreditDefaultSwapOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (CCreditDefaultSwap^ copy) : CInstrument(CCreditDefaultSwap::typeid)
{
#ifdef HANDLE
	_phCreditDefaultSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
    }
}
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (System::Type^ t) : CInstrument(CCreditDefaultSwap::typeid)
{
#ifdef HANDLE
	_phCreditDefaultSwap = NULL;
#endif
	if (!t->IsSubclassOf(CCreditDefaultSwap::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QuantLib::Handle<QuantLib::CreditDefaultSwap>& childNative, Object^ owner)  : CInstrument(CCreditDefaultSwap::typeid)
{
	_phCreditDefaultSwap = &childNative;
	_ppCreditDefaultSwap = &static_cast<boost::shared_ptr<QuantLib::CreditDefaultSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
    _CreditDefaultSwapOwner = owner;
}
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QuantLib::Handle<QuantLib::CreditDefaultSwap> childNative)  : CInstrument(CCreditDefaultSwap::typeid)
{
	_phCreditDefaultSwap = &childNative;
	_ppCreditDefaultSwap = &static_cast<boost::shared_ptr<QuantLib::CreditDefaultSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCreditDefaultSwap::CCreditDefaultSwap (QuantLib::CreditDefaultSwap childNative)  : CInstrument(CCreditDefaultSwap::typeid)
{
#ifdef HANDLE
	_phCreditDefaultSwap = NULL;
#endif
	_ppCreditDefaultSwap = new boost::shared_ptr<QuantLib::CreditDefaultSwap> (new QuantLib::CreditDefaultSwap (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppCreditDefaultSwap));
}
#endif

Cephei::QL::Instruments::CCreditDefaultSwap::~CCreditDefaultSwap ()
{
    if (_ppCreditDefaultSwap != NULL)
    {
	    delete _ppCreditDefaultSwap;
        _ppCreditDefaultSwap = NULL;
    }
}
Cephei::QL::Instruments::CCreditDefaultSwap::!CCreditDefaultSwap ()
{
    if (_ppCreditDefaultSwap != NULL)
    {
	    delete _ppCreditDefaultSwap;
    }
}
QuantLib::CreditDefaultSwap& Cephei::QL::Instruments::CCreditDefaultSwap::GetReference ()
{
    if (_ppCreditDefaultSwap == NULL) throw gcnew NativeNullException ();
	return **_ppCreditDefaultSwap;
}
boost::shared_ptr<QuantLib::CreditDefaultSwap>& Cephei::QL::Instruments::CCreditDefaultSwap::GetShared ()
{
    if (_ppCreditDefaultSwap == NULL) throw gcnew NativeNullException ();
	return *_ppCreditDefaultSwap;
}
QuantLib::CreditDefaultSwap* Cephei::QL::Instruments::CCreditDefaultSwap::GetPointer ()
{
    if (_ppCreditDefaultSwap == NULL) throw gcnew NativeNullException ();
	return &**_ppCreditDefaultSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CreditDefaultSwap>& Cephei::QL::Instruments::CCreditDefaultSwap::GetHandle ()
{
	if (_phCreditDefaultSwap == NULL)
	{
		_phCreditDefaultSwap = new Handle<QuantLib::CreditDefaultSwap> (*_ppCreditDefaultSwap);
	}
	return *_phCreditDefaultSwap;
}
#endif
bool Cephei::QL::Instruments::CCreditDefaultSwap::HasNative () 
{
	return (_ppCreditDefaultSwap != NULL);
}

Double Cephei::QL::Instruments::CCreditDefaultSwap::CouponLegBPS::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->couponLegBPS ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::CouponLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->couponLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::ICashFlow^>^ Cephei::QL::Instruments::CCreditDefaultSwap::Coupons::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::CashFlow> >& _rv = (std::vector<boost::shared_ptr<QuantLib::CashFlow> >&)(*_ppCreditDefaultSwap)->coupons ( );   
        CoVector<Cephei::QL::ICashFlow^>^ _nrv = gcnew CoVector<Cephei::QL::ICashFlow^>(gcnew CCashFlowVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::DefaultLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->defaultLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::FairSpread::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCreditDefaultSwap)->fairSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::ImpliedHazardRate (Double targetNPV, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Double>^ recoveryRate, Microsoft::FSharp::Core::FSharpOption<Double>^ accuracy)
{
    CYieldTermStructure^ _CdiscountCurve;
    CDayCounter^ _CdayCounter;
    try
    {
        QuantLib::Real _targetNPV = (QuantLib::Real)ValueHelper::Convert (targetNPV);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _recoveryRate = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (recoveryRate) ? (QuantLib::Real)ValueHelper::Convert (recoveryRate->Value) : 0.4); //9a
        QuantLib::Real _accuracy = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (accuracy) ? (QuantLib::Real)ValueHelper::Convert (accuracy->Value) : 1.0e-6); //9a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCreditDefaultSwap)->impliedHazardRate ( _targetNPV,  _discountCurve,  _dayCounter,  _recoveryRate,  _accuracy );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::ConventionalSpread (Double conventionalRecovery, Cephei::QL::Termstructures::IYieldTermStructure^ discountCurve, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    CYieldTermStructure^ _CdiscountCurve;
    CDayCounter^ _CdayCounter;
    try
    {
        QuantLib::Real _conventionalRecovery = (QuantLib::Real)ValueHelper::Convert (conventionalRecovery);
        _CdiscountCurve = safe_cast<CYieldTermStructure^> (discountCurve);
        _CdiscountCurve->Lock();
        Handle<QuantLib::YieldTermStructure>& _discountCurve = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CdiscountCurve->GetHandle ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCreditDefaultSwap)->conventionalSpread ( _conventionalRecovery,  _discountCurve,  _dayCounter );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdiscountCurve != nullptr) _CdiscountCurve->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Boolean Cephei::QL::Instruments::CCreditDefaultSwap::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCreditDefaultSwap)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::Notional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->notional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CCreditDefaultSwap::PaysAtDefaultTime::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCreditDefaultSwap)->paysAtDefaultTime ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Instruments::CCreditDefaultSwap::SettlesAccrual::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCreditDefaultSwap)->settlesAccrual ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Protection::SideEnum Cephei::QL::Instruments::CCreditDefaultSwap::Side::get ()
{
    try
    {
    	QuantLib::Protection::Side _rv = (QuantLib::Protection::Side)(*_ppCreditDefaultSwap)->side ( );   
        QL::Protection::SideEnum _nrv = (QL::Protection::SideEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::FairUpfront::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCreditDefaultSwap)->fairUpfront ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CCreditDefaultSwap::ProtectionEndDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCreditDefaultSwap)->protectionEndDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CCreditDefaultSwap::ProtectionStartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppCreditDefaultSwap)->protectionStartDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::RunningSpread::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCreditDefaultSwap)->runningSpread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Microsoft::FSharp::Core::FSharpOption<Double>^ Cephei::QL::Instruments::CCreditDefaultSwap::Upfront::get ()
{
    try
    {
    	boost::optional<QuantLib::Rate> _rv = (boost::optional<QuantLib::Rate>)(*_ppCreditDefaultSwap)->upfront ( );   
        Microsoft::FSharp::Core::FSharpOption<Double>^ _nrv = gcnew Microsoft::FSharp::Core::FSharpOption<Double> (ValueHelper::Convert (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::UpfrontBPS::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->upfrontBPS ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CCreditDefaultSwap::UpfrontNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCreditDefaultSwap)->upfrontNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICreditDefaultSwap^ Cephei::QL::Instruments::CCreditDefaultSwap_Factory::Create (QL::Protection::SideEnum side, Double notional, Double upfront, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ upfrontDate, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCreditDefaultSwap ( side,  notional,  upfront,  spread,  schedule,  paymentConvention,  dayCounter,  settlesAccrual,  paysAtDefaultTime,  protectionStart,  upfrontDate,  claim,  QL_Pricer);
}
Cephei::QL::Instruments::ICreditDefaultSwap^ Cephei::QL::Instruments::CCreditDefaultSwap_Factory::Create (QL::Protection::SideEnum side, Double notional, Double spread, Cephei::QL::Times::ISchedule^ schedule, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<Boolean>^ settlesAccrual, Microsoft::FSharp::Core::FSharpOption<Boolean>^ paysAtDefaultTime, Microsoft::FSharp::Core::FSharpOption<DateTime>^ protectionStart, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Instruments::IClaim^>^ claim, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCreditDefaultSwap ( side,  notional,  spread,  schedule,  paymentConvention,  dayCounter,  settlesAccrual,  paysAtDefaultTime,  protectionStart,  claim,  QL_Pricer);
}
